package gl.android.biyiqing.competent;

import gl.adapter.SimpleAdapter.OnItemChildViewClick;
import gl.android.biyiqing.GreetActivity;
import gl.android.biyiqing.R;
import gl.android.biyiqing.api.API;
import gl.android.biyiqing.bean.Result;
import gl.android.biyiqing.event.EventOnFollow;
import gl.android.biyiqing.fragment.BaseFragment;
import gl.common.BaseControl;
import gl.common.UserManger;
import gl.uitls.AndroidSystemUtils;
import gl.uitls.AndroidUtil;
import gl.uitls.GsonUtils;
import gl.uitls.Lg;
import gl.uitls.Tip;

import java.util.Map;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import de.greenrobot.event.EventBus;

import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;

/**
 * 点击人物打招呼
 * 
 * @author geliang
 * 
 */
public class OnToGreetItemClickListener implements OnItemChildViewClick ,View.OnClickListener{
	private JSONArray dataJsonarray;
	private Context contetxt;

	public OnToGreetItemClickListener(Context contetxt, JSONArray dataJsonarray) {
		this.dataJsonarray = dataJsonarray;
		this.contetxt = contetxt;
	}
	public OnToGreetItemClickListener(Context mContext, String userID
			) {
		JSONArray ja = new JSONArray();
		final JSONObject value = new JSONObject();
		try {
			value.put(BaseFragment.KEY_TO_USER_ID, userID);
		} catch (JSONException e) {
			e.printStackTrace();
		}
		ja.put(value);
		dataJsonarray = ja;
		this.contetxt = mContext;
	}
	@Override
	public void onClick(View view, int position) {
		Lg.i("onClick:postion" + position);
		if (view.getId() == R.id.hi) {
			try {
				Intent intent = new Intent(contetxt, GreetActivity.class);
				Bundle bundle = new Bundle();
				bundle.putString(BaseFragment.KEY_TO_USER_ID,
						GsonUtils.JSON2MapArrayList(dataJsonarray)
								.get(position).get(BaseFragment.KEY_TO_USER_ID));
				intent.putExtras(bundle);
				contetxt.startActivity(intent);
			} catch (Exception e) {
				e.printStackTrace();
			}

		}
		else if (view.getId() == R.id.iv_fav||view.getId() == R.id.follow) {
			follow(position);
		}
		else if (view.getId() == R.id.iv_call) {
			call(position);
		}
		else if (view.getId() == R.id.iv_sendmsg) {
			sendmsg(position);
		}
	}
	private void call(int position) {
		final Map<String, String> map = GsonUtils.JSON2MapArrayList(dataJsonarray)
				.get(position);
		final String specialissue = map.get("supervice");
		AndroidSystemUtils.Call(contetxt, specialissue);
		
	}
	public void sendmsg(int position) {
		final EditText et = new EditText(contetxt);
		final Map<String, String> map = GsonUtils.JSON2MapArrayList(dataJsonarray)
				.get(position);
		final String specialissue = map.get("hideSMS");
		AndroidUtil.genCustomViewDialog(contetxt, et, "请输入短信内容", "私密短信",
				new DialogInterface.OnClickListener() {

					@Override
					public void onClick(DialogInterface dialog, int which) {
						dialog.dismiss();
						try {
							if (et.getText() != null ) {
								AndroidSystemUtils.sendSMS(contetxt,  specialissue , et.getText().toString());
								Tip.show("短信已发送");
							}
						} catch (Exception e) {
							e.printStackTrace();
						}

					}
				});
	}

	/**
	 * 关注某人
	 * @param position
	 */
	private void follow(int position) {
		String userid = GsonUtils.JSON2MapArrayList(dataJsonarray)
				.get(position).get(BaseFragment.KEY_USER_ID);

		BaseControl.get(API.URL + "TakeFriend?userid=%s&fuserid=%s",
				new ByqAjaxCallProxy() {
					@Override
					public void onSuccess(String t) {
						super.onSuccess(t);
						try {
							Result result = unformat(t);
							if ("0".equals(result.getMsg())) {
								Tip.show("已经关注");
							} else if ("1".equals(result.getMsg())) {
								Tip.show("关注失败  ");
							} else if ("2".equals(result.getMsg())) {
								Tip.show("关注成功！");
								EventBus.getDefault().post(new EventOnFollow());
							}
						} catch (Exception e) {
							e.printStackTrace();
						}

					}
				}, new Object[] { userid, UserManger.getInstance().userid });

	}
	@Override
	public void onClick(View v) {
		onClick(v,0);
		
	}
}
